package com.ldk.framework.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ldk.base.framework.dao.IBaseDAO;
import com.ldk.base.framework.service.impl.BaseService;
import com.ldk.base.framework.util.DateUtil;
import com.ldk.framework.entity.RatedPassengercomplaint;
import com.ldk.framework.service.IComplaintImagesService;
import com.ldk.framework.service.IComplaintService;
import com.ldk.framework.service.IRatedPassengercomplaintService;
import com.ldk.framework.service.IServiceTripService;
import com.ldk.framework.dao.IComplaintDAO;
import com.ldk.framework.entity.Complaint;
import com.ldk.framework.entity.ComplaintImages;
import com.ldk.framework.entity.ServiceTrip;

/**
* ComplaintService接口实现类
* @author xiangwl
*/
@Service
public class ComplaintService extends BaseService<Complaint> implements IComplaintService {

	@Autowired
	private IComplaintDAO complaintDAO;
	@Autowired
	private IComplaintImagesService complaintImagesService;
	@Autowired
	private IServiceTripService serviceTripService;
	@Autowired
	protected IRatedPassengercomplaintService ratedPassengercomplaintService;

	@Override
	protected IBaseDAO<Complaint> getBaseDao() {
     	return this.complaintDAO;
	}

	@Override
	@Transactional
	public Boolean userComplaint(String dbKey, ServiceTrip trip, Complaint complaint, List<ComplaintImages> images) throws Exception {
		if(complaint == null) return false;
		ServiceTrip tripEntity = new ServiceTrip();
		tripEntity.setTripNo(trip.getTripNo());
		tripEntity.setHasComplaint(true);
		int row =serviceTripService.updateByKey(dbKey, tripEntity);
		row = insert(dbKey, complaint);
		//添加投诉图片
		if(images != null && !images.isEmpty() && row > 0){
			for (ComplaintImages image : images) {
				complaintImagesService.insert(dbKey, image);
			}
		}
		if(row>0){
			RatedPassengercomplaint ratedPassengercomplaint=new RatedPassengercomplaint();
			ratedPassengercomplaint.setCompanyId("5201HDCX8779");
			ratedPassengercomplaint.setOrderId(trip.getTripNo());
			ratedPassengercomplaint.setComplaintTime(Long.parseLong(DateUtil.DatetoString(new Date(), "yyyyMMddHHmmss")));
			ratedPassengercomplaint.setDetail(complaint.getContent());
			ratedPassengercomplaint.setResult("");
			ratedPassengercomplaint.setComplaintTime2(0L);
			ratedPassengercomplaintService.insertUpper(dbKey, ratedPassengercomplaint);
		}
		return row > 0;
	}

}
